// pages/createPatient/bind/bind.js
// 获取绑定人信息，进而绑定
const api = require('../../../config/api')
const request = require('../../../utils/request')
const { parse } = require('../../../utils/util')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    codetext: '发送验证码',
    second: 60,
    captcha: '', // 验证码
    phone: '',
    disabled: false,
    babyData: {},
    type: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const babyData = parse(options.babyData)
    let type = ''
    type = options?.type
    this.setData({
      babyData,
      type: Number(type),
    })
  },
  // 验证码
  code(e) {
    this.setData({
      captcha: e.detail.value,
    })
  },
  // 发送验证码
  sendCode: async function () {
    const { disabled } = this.data
    const { isBandCurrentUser } = this.data.babyData
    if (isBandCurrentUser) {
      wx.showToast({
        title: '您已经绑定过该档案',
        icon: 'none',
      })
      return
    }

    if (!disabled) {
      const { bindUserList } = this.data.babyData
      const res = await request.get(api.sendPhoneCode, {
        type: 3,
        phone: bindUserList[0].phone,
      })
      if (res.httpStatus === 200) {
        this.SetTime()
      } else {
        wx.showToast({
          title: res.msg ? res.msg : '发送失败',
          icon: 'none',
        })
      }
    }
  },
  // 倒计时
  SetTime: function () {
    if (this.data.second == 0) {
      this.setData({
        disabled: false,
        codetext: '重新发送',
        second: 60,
      })
    } else {
      this.setData({
        disabled: true,
        codetext: this.data.second + 's',
        second: this.data.second - 1,
      })
      setTimeout(this.SetTime, 1000)
    }
  },
  // 验证验证码 -验证码正确 -绑定
  checkCode: async function () {
    const { captcha, type } = this.data
    const { bindUserList, id } = this.data.babyData
    const userId = wx.getStorageSync('userId')

    if(this.loading){
      return
    }

    if (type === 1) {
      if (captcha === '') {
        wx.showToast({
          icon: 'none',
          title: '请输入验证码',
        })
        return
      }
      const res = await request.get(api.checkPhoneCode, {
        captcha,
        phone: bindUserList[0].phone,
        type: 3,
      })
      if (res.data) {
        this.loading = true
        const bindres = await request.post(api.bindPatient, {
          patientId: id,
          userId,
        })
        if (bindres.httpStatus === 200) {
          app.getPatientList()
          setTimeout(() => {
            app.handleGoPage()
            setTimeout(()=>{
              this.loading = false
            },1500)
          }, 400)
        }
      } else {
        wx.showToast({
          title: '验证码错误',
          icon: 'none',
        })
        this.loading = false
      }
    } else {
      this.loading = true
      const bindres = await request.post(api.bindPatient, {
        patientId: id,
        userId,
      })
      if (bindres.httpStatus === 200) {
        app.getPatientList()
        setTimeout(() => {
          app.handleGoPage()
          setTimeout(()=>{
            this.loading = false
          },1500)
        }, 400)
      }
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
})
